package com.common.UITags;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class NumberInputTag extends TagSupport{
	private static final long serialVersionUID = 1L;
	
	private String id;//id
	private String name;//name
	private String label;//标签
	private String defaultvalue;//默认值
	private String dataOptions;//数据验证规则
	private String precision="2";//小数点后保留的位数
	private String readonly;
	
	
	
	@Override
	public int doStartTag() throws JspException {
		try{
			JspWriter out = pageContext.getOut();
			StringBuffer sBF = new StringBuffer();
			StringBuffer sBFL = new StringBuffer();
			sBFL.setLength(0);
			if(!(label==null||"".equals(label))){
				sBFL.append("<span style=\"font-size: small;vertical-align: middle;font-weight: bold;color:#535C5E;display:inline-block;width:43%;text-align:right;\">" +label);
				sBFL.append(":&nbsp;&nbsp;</span>");
			}
			sBF.setLength(0);
			sBF.append("<input type = \"text\" class=\"easyui-numberbox ");
			if(dataOptions!=null&&!"".equals(dataOptions)){
				sBF.append(" easyui-validatebox ");
			}
			sBF.append("\" ");
			if(dataOptions == null){
				dataOptions = "";
			}
			sBF.append(" data-options=\"");
			sBF.append("precision:"+this.precision+",");
			if("true".equals(readonly)){
				sBF.append("readonly:true,");
			}
			sBF.append(this.dataOptions);
			sBF.append("\" ");
			if(!(id==null ||"".equals(id))){
				sBF.append(" id = \""+id+"\" ");
			}
			if(!(name==null ||"".equals(name))){
				sBF.append(" name = \""+name+"\" ");
			}else{
				sBF.append(" name = \""+id+"\" ");
			}
			
			if(!(defaultvalue==null || "".equals(defaultvalue))){
				sBF.append(" value = \""+defaultvalue+"\" ");
			}
			sBF.append("style=\"");
			//为了好看设置宽度为表格宽度的55%
			if(this.getParent() instanceof FormTableTag){
				sBF.append(" width: 100%;");
			}
			sBF.append("text-align: center; ");
			sBF.append("\"");
			sBF.append("></input>");
			if(this.getParent() instanceof FormTableTag){
				out.print("<td width=\"33%\">" + sBFL.toString() + "<div style=\"width:55%;display:inline-block;\">" + sBF.toString()+"</div>" + "</td>");
			}else{
				out.print(sBF.toString());
			}
		} catch (IOException e) {
			throw new JspException("标签往前台写数据时异常");
		}catch(Exception e){
			throw new JspException("其他异常");
		}
		return SKIP_BODY;
	}
	
	
	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}


	//get和set方法
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}




	public String getDefaultvalue() {
		return defaultvalue;
	}


	public void setDefaultvalue(String defaultvalue) {
		this.defaultvalue = defaultvalue;
	}




	public String getDataOptions() {
		return dataOptions;
	}


	public void setDataOptions(String dataOptions) {
		this.dataOptions = dataOptions;
	}


	public String getPrecision() {
		return precision;
	}


	public void setPrecision(String precision) {
		this.precision = precision;
	}


	public String getReadonly() {
		return readonly;
	}


	public void setReadonly(String readonly) {
		this.readonly = readonly;
	}
	
	
}
